//
//  HJSettlementInfoView.m
//  GAMCAfterSales
//
//  Created by 贺佳 on 2018/5/15.
//  Copyright © 2018年 glendale. All rights reserved.
//

#import "HJSettlementInfoView.h"

#import "HJSettlementRepairProjectView.h"
#import "HJSettlementRepairPartsView.h"
#import "HJSettlementAdditionalItemsView.h"
#import "HJSettlementPreferentialProjectView.h"

@interface HJSettlementInfoView()
/**
 
 */
@property(nonatomic,strong)UIPopoverController*popCon;
/**
 当前选择时间按钮
 */
@property(nonatomic,weak)UIButton*currentBtn;
/**
 项目数组
 */
@property(nonatomic,strong)NSArray*projectAry;
/**
 项目详细视图
 */
@property(nonatomic,weak)UIView*projectMoreView;
/**
 项目详细头部视图
 */
@property(nonatomic,weak)UIView*projectHeaderView;
/**
 费用详情视图
 */
@property(nonatomic,weak)UIView*infoDetailView;
/**
 维修项目视图
 */
@property(nonatomic,weak)HJSettlementRepairProjectView*projectView;
/**
 维修配件视图
 */
@property(nonatomic,weak)HJSettlementRepairPartsView*repairPartsView;
/**
 附加项目视图
 */
@property(nonatomic,weak)HJSettlementAdditionalItemsView*additionalItemsView;
/**
 优惠项目视图
 */
@property(nonatomic,weak)HJSettlementPreferentialProjectView*preferentialProjectView;
@end
@implementation HJSettlementInfoView
-(NSArray *)projectAry
{
    if (!_projectAry) {
        _projectAry=@[@"工时费用:",@"材料费用:",@"其他费用:",@"汇总金额:",@"折扣金额:",@"减免金额:",@"优惠劵金额:",@"应收总金额:"];
    }
    return _projectAry;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}
-(void)setUI{
    UILabel*titleLabel=[[UILabel alloc]init];
    [self addSubview:titleLabel];
    titleLabel.text=@"结算信息:";
    titleLabel.font=HJFont(13);
    WEAKSELF;
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf);
        make.top.mas_equalTo(weakSelf);
    }];
    NSInteger row=4;
    
    CGFloat labelH=20;
    CGFloat w=250;
    CGFloat h=weakSelf.projectAry.count/2*labelH;
    if (weakSelf.projectAry.count%row) {
        h+=labelH;
    }
    //信息详情
    UIView*infoDetailView=[[UIView alloc]init];
    [self addSubview:infoDetailView];
    self.infoDetailView=infoDetailView;
    [infoDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf);
        make.top.mas_equalTo(titleLabel.mas_bottom).offset(10);
        make.height.mas_equalTo(h);
        make.right.mas_equalTo(weakSelf.mas_centerX);
    }];
    for (int i=0; i<self.projectAry.count; i++) {
        UILabel*label=[[UILabel alloc]init];
        [infoDetailView addSubview:label];
        label.text=self.projectAry[i];
        label.font=HJFont(13);
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(infoDetailView).offset(i/row*(w+20));
            make.top.mas_equalTo(infoDetailView).offset(i%row*labelH);
            make.height.mas_equalTo(labelH);
        }];
    }
    
    //提醒视图
    UIView*explainView=[[UIView alloc]init];
    [self addSubview:explainView];
    explainView.backgroundColor = [[UIColor alloc]initWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [explainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.mas_centerX);
        make.top.right.mas_equalTo(weakSelf);
        make.bottom.mas_equalTo(infoDetailView);
    }];
    UILabel*explainLabel=[[UILabel alloc]init];
    [explainView addSubview:explainLabel];
    explainLabel.text=@"提醒";
    explainLabel.font=[UIFont boldSystemFontOfSize:13];
    [explainLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(explainView).offset(5);
        make.top.right.mas_equalTo(explainView);
        make.height.mas_equalTo(30);
    }];
    //三日电话时间
    UIView*nearView=[self setSelectView:@"三日电访时间"];
    [explainView addSubview:nearView];
    [nearView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(explainView);
        make.top.mas_equalTo(explainLabel.mas_bottom);
        make.right.mas_equalTo(explainView.mas_centerX);
        make.height.mas_equalTo(30);
    }];
    
    //下次保养时间
    UIView*nextView=[self setSelectView:@"下次保养时间"];
    [explainView addSubview:nextView];
    [nextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(explainView);
        make.centerY.mas_equalTo(nearView);
        make.left.mas_equalTo(explainView.mas_centerX);
        make.height.mas_equalTo(nearView);
    }];
    //不回访原因
    UIView*resonView=[self setTextView:@"不回访原因"];
    [explainView addSubview:resonView];
    [resonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(nearView);
        make.top.mas_equalTo(nearView.mas_bottom).offset(8);
    }];
    
    //下次保养里程
    UIView*nextMileageView=[self setTextView:@"下次保养里程"];
    [explainView addSubview:nextMileageView];
    [nextMileageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(nextView);
        make.centerY.mas_equalTo(resonView);
    }];
    
    //项目table
    UIView*projectTableView=[[UIView alloc]init];
    [self addSubview:projectTableView];
    projectTableView.backgroundColor = [[UIColor alloc]initWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [projectTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(weakSelf);
        make.bottom.mas_equalTo(weakSelf).inset(30);
        make.top.mas_equalTo(explainView.mas_bottom).offset(30);
    }];
    //项目头部
    NSArray*ary=@[@"维修项目",@"维修配件",@"附加项目",@"优惠项目"];
    UIView*projectHeaderView=[[UIView alloc]init];
    [projectTableView addSubview:projectHeaderView];
    self.projectHeaderView=projectHeaderView;
    [projectHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(projectTableView);
        make.height.mas_equalTo(45);
    }];
    
    //内容
    UIView*projectMoreView=[[UIView alloc]init];
    [projectTableView addSubview:projectMoreView];
    self.projectMoreView=projectMoreView;
    [projectMoreView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(projectTableView);
        make.top.mas_equalTo(projectHeaderView.mas_bottom);
    }];
    //维修项目
    HJSettlementRepairProjectView*projectView=[HJSettlementRepairProjectView new];
    self.projectView=projectView;
    [projectMoreView addSubview:projectView];
    [projectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(projectMoreView);
        
    }];
    //维修配件
    HJSettlementRepairPartsView*repairPartsView=[HJSettlementRepairPartsView new];
    self.repairPartsView=repairPartsView;
    [projectMoreView addSubview:repairPartsView];
    [repairPartsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(projectMoreView);
    }];
    //附加项目
    HJSettlementAdditionalItemsView*additionalItemsView=[HJSettlementAdditionalItemsView new];
    self.additionalItemsView=additionalItemsView;
    [projectMoreView addSubview:additionalItemsView];
    [additionalItemsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(projectMoreView);
    }];
    //优惠项目
    HJSettlementPreferentialProjectView*preferentialProjectView=[HJSettlementPreferentialProjectView new];
    self.preferentialProjectView=preferentialProjectView;
    [projectMoreView addSubview:preferentialProjectView];
    [preferentialProjectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(projectMoreView);
    }];
    
    
    CGFloat titleW=100;
    int i =0;
    for (NSString*title in ary) {
        UIButton*btn=[[UIButton alloc]init];
        btn.tag=i;
        [btn addTarget:self action:@selector(clickSelectProjectView:) forControlEvents:UIControlEventTouchUpInside];
        [projectHeaderView addSubview:btn];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithRed:239.0/255 green:139.0/255 blue:82.0/255 alpha:1] forState:UIControlStateSelected];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.mas_equalTo(projectHeaderView);
            make.width.mas_equalTo(titleW);
            make.left.mas_equalTo(projectHeaderView).offset(i*(titleW+10));
        }];
        if (i==0) {
            [self clickSelectProjectView:btn];
        }
        i++;
    }
    
    
    
}
-(void)clickSelectProjectView:(UIButton*)btn
{
    for (UIButton*subBtn in self.projectHeaderView.subviews) {
        subBtn.selected=NO;
    }
    btn.selected=YES;
    
    for (UIView*subView in self.projectMoreView.subviews) {
        subView.hidden=YES;
    }
    UIView*subView=self.projectMoreView.subviews[btn.tag];
    subView.hidden=NO;
}
-(UIView*)setTextView:(NSString*)title{
    UIView*subView=[[UIView alloc]init];
    [self addSubview:subView];
//    [subView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(i?weakSelf.mas_centerX:weakSelf);
//        make.top.mas_equalTo(weakSelf).offset(20);
//        make.size.mas_equalTo(CGSizeMake(w, H));
//    }];
    UILabel*leftLabel=[[UILabel alloc]init];
    leftLabel.text=title;
    leftLabel.font=[UIFont systemFontOfSize:13];
    [subView addSubview:leftLabel];
    [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(subView);
        make.width.mas_equalTo(80);
    }];
    UIView*bgView=[[UIView alloc]init];
    [subView addSubview:bgView];
    bgView.backgroundColor=[UIColor grayColor];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(subView);
        make.left.mas_equalTo(leftLabel.mas_right).offset(5);
        make.right.mas_equalTo(subView).offset(-10);
    }];
    
    UITextField*textField=[[UITextField alloc]init];
    textField.font=HJFont(13);
    textField.backgroundColor=[UIColor whiteColor];
    [subView addSubview:textField];
    textField.placeholder=title;
    textField.layer.borderColor=[UIColor grayColor].CGColor;
    textField.layer.borderWidth=0.5;
    textField.layer.masksToBounds=YES;
    textField.textAlignment=NSTextAlignmentCenter;
    [textField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.bottom.mas_equalTo(bgView).inset(1);
    }];
    return subView;
}
-(UIView*)setSelectView:(NSString*)title{
    UIView*subView=[[UIView alloc]init];
    
    UILabel*leftLabel=[[UILabel alloc]init];
    leftLabel.text=title;
    leftLabel.font=[UIFont systemFontOfSize:13];
    [subView addSubview:leftLabel];
    [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(subView);
        make.width.mas_equalTo(80);
    }];
    
    UIView*bgView=[[UIView alloc]init];
    bgView.backgroundColor=[UIColor grayColor];
    [subView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(subView);
        make.left.mas_equalTo(leftLabel.mas_right).offset(5);
        make.right.mas_equalTo(subView).offset(-10);
    }];
    UIButton*btn=[[UIButton alloc]init];
    [subView addSubview:btn];
    btn.layer.borderColor=[UIColor grayColor].CGColor;
    btn.layer.borderWidth=0.5;
    btn.layer.masksToBounds=YES;
    [btn setImage:[UIImage imageNamed:@"下拉框.png"] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickSelect:) forControlEvents:UIControlEventTouchUpInside];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(bgView).inset(1);
    }];
    UILabel*contentLabel=[[UILabel alloc]init];
    [btn addSubview:contentLabel];
    contentLabel.tag=10;
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(btn);
    }];
    contentLabel.textAlignment=NSTextAlignmentCenter;
    contentLabel.text=@"请选择";
    contentLabel.textColor=[UIColor grayColor];
    contentLabel.font=HJFont(15);
    
    return subView;
}
-(void)pickerBut1:(id)sender
{
    UILabel*label=[self.currentBtn viewWithTag:10];
    label.text=@"请选择";
    [self.popCon dismissPopoverAnimated:YES];
}
-(void)dateChanged:(id)sender{
    UIDatePicker *contro = (UIDatePicker*)sender;
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
     NSString*dateString = [formatter stringFromDate:contro.date];
    if ([self.currentBtn.currentTitle containsString:@"三日电访"]) {
        if ([_delegate respondsToSelector:@selector(settlementInfoView:changePhoneTime:)]) {
            [_delegate settlementInfoView:self changePhoneTime:dateString];
        }
        HJLog(@"点击选择电访时间");
    }else if ([self.currentBtn.currentTitle containsString:@"下次保养时间"]){
        HJLog(@"点击选择下次保养时间");
        if ([_delegate respondsToSelector:@selector(settlementInfoView:changeMaintainTime:)]) {
            [_delegate settlementInfoView:self changeMaintainTime:dateString];
        }
    }
    UILabel*label=[self.currentBtn viewWithTag:10];
    label.text=dateString;
   
}
-(void)pickerBut:(id)sender
{
    NSDate *senddate=[NSDate date];
    NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd"];
     NSString*dateString = [dateformatter stringFromDate:senddate];
    [self.popCon dismissPopoverAnimated:YES];
    if ([self.currentBtn.currentTitle containsString:@"三日电访"]) {
        if ([_delegate respondsToSelector:@selector(settlementInfoView:changePhoneTime:)]) {
            [_delegate settlementInfoView:self changePhoneTime:dateString];
        }
        HJLog(@"点击选择电访时间");
    }else if ([self.currentBtn.currentTitle containsString:@"下次保养时间"]){
        HJLog(@"点击选择下次保养时间");
        if ([_delegate respondsToSelector:@selector(settlementInfoView:changeMaintainTime:)]) {
            [_delegate settlementInfoView:self changeMaintainTime:dateString];
        }
    }
    UILabel*label=[self.currentBtn viewWithTag:10];
    label.text=dateString;
}
-(void)clickSelect:(UIButton*)btn{
    self.currentBtn=btn;
    UIDatePicker*datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, 280, 160)];
    datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:NSLocalizedString(@"Chinese", @"")];
    datePicker.datePickerMode = UIDatePickerModeDate;
    
//    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//    fmt.dateFormat = @"yyyy-MM-dd";
    NSDate *minDate =[NSDate date];
    NSDate *  timeDate;
    if ([btn.currentTitle containsString:@"三日"]) {
        timeDate= [NSDate dateWithTimeInterval:3*24*60*60 sinceDate:[NSDate date]];//3天
    }else if ([btn.currentTitle containsString:@"保养"]){
         timeDate= [NSDate dateWithTimeInterval:90*24*60*60 sinceDate:[NSDate date]];//3个月
    }
    //设置日期最大及最小值
    datePicker.maximumDate = timeDate;
    datePicker.minimumDate = minDate;
    [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
    //
   UIViewController*panleCon = [[UIViewController alloc] init];
    [panleCon.view addSubview:datePicker];
    UIPopoverController*popCon = [[UIPopoverController alloc] initWithContentViewController:panleCon];
    popCon.popoverContentSize = CGSizeMake(280.0f, 200.0f);
    self.popCon=popCon;
    [popCon presentPopoverFromRect:CGRectMake(20, 0, 10, 40) inView:btn permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    UIButton *pickerBut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [pickerBut setBackgroundColor:[UIColor clearColor]];
    [pickerBut setTitle:@"今 天" forState:UIControlStateNormal];
    [pickerBut setFrame:CGRectMake(20, 165, 80, 30)];
    [pickerBut addTarget:self action:@selector(pickerBut:) forControlEvents:UIControlEventTouchUpInside];
    [panleCon.view addSubview:pickerBut];
    UIButton *pickerBut1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [pickerBut1 setBackgroundColor:[UIColor clearColor]];
    [pickerBut1 setTitle:@"清 除" forState:UIControlStateNormal];
    [pickerBut1 setFrame:CGRectMake(180, 165, 80, 30)];
    [pickerBut1 addTarget:self action:@selector(pickerBut1:) forControlEvents:UIControlEventTouchUpInside];
    [panleCon.view addSubview:pickerBut1];
    
   
}
-(void)textFieldChange:(UITextField*)textField{
    if ([textField.placeholder containsString:@"原因"]) {
        HJLog(@"不回访原因");
        if ([_delegate respondsToSelector:@selector(settlementInfoView:changeReson:)]) {
            [_delegate settlementInfoView:self changeReson:textField.text];
        }
    }else if ([textField.placeholder containsString:@"里程"]){
        if ([_delegate respondsToSelector:@selector(settlementInfoView:changeMileage:)]) {
            [_delegate settlementInfoView:self changeMileage:textField.text];
        }
        HJLog(@"下次保养里程");
    }
}
-(void)setRepairSettlementModel:(HJRepairSettlementModel *)repairSettlementModel
{
    _repairSettlementModel=repairSettlementModel;
    
    self.projectView.LABOURLIST=repairSettlementModel.LABOURLIST;
    self.repairPartsView.PARTLIST=repairSettlementModel.PARTLIST;
    self.additionalItemsView.ADDITEM=repairSettlementModel.ADDITEM;
    self.preferentialProjectView.FAVORABLE=repairSettlementModel.FAVORABLE;
    //@[@"工时费用:",@"材料费用:",@"其他费用:",@"汇总金额:",@"折扣金额:",@"减免金额:",@"优惠劵金额:",@"应收总金额:"];
    int i =0;
    for (UILabel*label in self.infoDetailView.subviews) {
        NSString*content=label.text;
        switch (i) {
            case 0:
                content=[content stringByAppendingString:repairSettlementModel.LABOUR_AMOUNT];
                label.text=content;
                break;
            case 1:
                content=[content stringByAppendingString:repairSettlementModel.PART_AMOUNT];
                label.text=content;
                break;
            case 2:
                content=[content stringByAppendingString:repairSettlementModel.ADDITEMA_AMOUNT];
                label.text=content;
                break;
            case 3:
                content=[content stringByAppendingString:repairSettlementModel.ALL_AMOUNT];
                label.text=content;
                break;
            case 4:
                content=[content stringByAppendingString:repairSettlementModel.DISCOUNT_AMOUNT];
                label.text=content;
                break;
            case 5:
                content=[content stringByAppendingString:repairSettlementModel.REDUCTION];
                label.text=content;
                break;
            case 6:
                content=[content stringByAppendingString:repairSettlementModel.DISCOUNT];
                label.text=content;
                break;
            case 7:
                content=[content stringByAppendingString:repairSettlementModel.REAL_RECEVIE];
                label.text=content;
                break;
                
            default:
                break;
        }
        i++;
       
    }
}
@end
